﻿//头文件
#include<stdio.h>
#include<string.h>
#include"head.h"
#include<stdlib.h>
#include<math.h>

//函数

//显示各分数段情况函数
void DuanDis(struct student* stu, int* i)
{
	int j = 0, a[NN] = { 0 }, b[NN] = { 0 }, c[NN] = { 0 }, d[NN] = { 0 }, e[NN] = { 0 }, v = 0, w = 0, x = 0, y = 0, z = 0;
	system("cls");
	for (j = 0;j < *i;j++)
	{
		if ((stu + j)->f_totalGrade >= 90 && (stu + j)->f_totalGrade <= 100)
		{
			a[v] = j + 1;
			v++;
		}
		else if ((stu + j)->f_totalGrade >= 80 && (stu + j)->f_totalGrade < 90)
		{
			b[w] = j + 1;
			w++;
		}
		else if ((stu + j)->f_totalGrade >= 70 && (stu + j)->f_totalGrade < 80)
		{
			c[x] = j + 1;
			x++;
		}
		else if ((stu + j)->f_totalGrade >= 60 && (stu + j)->f_totalGrade < 70)
		{
			d[y] = j + 1;
			y++;
		}
		else if ((stu + j)->f_totalGrade >= 0 && (stu + j)->f_totalGrade < 60)
		{
			e[z] = j + 1;
			z++;
		}
		else
		{}
	}
	printf("学生总评成绩分数段情况：\n");
	printf("90~100分（共%d人）：\n", v);
	printf("学号\t\t\t姓名\t\t总评成绩\n");
	for (v = 0; a[v] != 0;v++)
	{
		printf("%d\t\t%s\t\t%.1f\n", (stu + a[v] - 1)->i_id, (stu + a[v] - 1)->s_Name, (stu + a[v] - 1)->f_totalGrade);
	}
	printf("80~90分（共%d人）：\n",w);
	printf("学号\t\t\t姓名\t\t总评成绩\n");
	for (w = 0; b[w] != 0; w++)
	{
		printf("%d\t\t%s\t\t%.1f\n", (stu + b[w] - 1)->i_id, (stu + b[w] - 1)->s_Name, (stu + b[w] - 1)->f_totalGrade);
	}
	printf("70~80分（共%d人）：\n", x);
	printf("学号\t\t\t姓名\t\t总评成绩\n");
	for (x = 0; c[x] != 0; x++)
	{
		printf("%d\t\t%s\t\t%.1f\n", (stu + c[x] - 1)->i_id, (stu + c[x] - 1)->s_Name, (stu + c[x] - 1)->f_totalGrade);
	}
	printf("60~70分（共%d人）：\n", y);
	printf("学号\t\t\t姓名\t\t总评成绩\n");
	for (y = 0; d[y] != 0; y++)
	{
		printf("%d\t\t%s\t\t%.1f\n", (stu + d[y] - 1)->i_id, (stu + d[y] - 1)->s_Name, (stu + d[y] - 1)->f_totalGrade);
	}
	printf("0~60分（共%d人）：\n", z);
	printf("学号\t\t\t姓名\t\t总评成绩\n");
	for (z = 0; e[z] != 0; z++)
	{
		printf("%d\t\t%s\t\t%.1f\n", (stu + e[z] - 1)->i_id, (stu + e[z] - 1)->s_Name, (stu + e[z] - 1)->f_totalGrade);
	}
}

//寻找最高最低分、输出名单函数
void MaxMin(struct student* stu, int* i)
{
	float fMax = 0, fMin = 100;
	int j = 0;
	for (j = 0;j < *i; j++)
	{
		fMax = ((stu + j)->f_totalGrade > fMax) ? (stu + j)->f_totalGrade : fMax;
		fMin = ((stu + j)->f_totalGrade < fMin) ? (stu + j)->f_totalGrade : fMin;
	}
	system("cls");
	printf("最高分为%.1f分\n",fMax);
	printf("最高分名单：\n");
	printf("学号\t\t\t姓名\t\t总评成绩\n");
	for (j = 0;j < *i; j++)
	{
		if (fMax == (stu + j)->f_totalGrade)
			printf("%d %s %f\n", (stu + j)->i_id, (stu + j)->s_Name, (stu + j)->f_totalGrade);
	}
	printf("最低分为%.1f\n", fMin);
	printf("最低分名单：\n");
	for (j = 0;j < *i; j++)
	{
		if (fMin == (stu + j)->f_totalGrade)
			printf("%d %s %f\n", (stu + j)->i_id, (stu + j)->s_Name, (stu + j)->f_totalGrade);
	}
}

//求均值、方差函数
void Avera(struct student* stu, int* i)
{
	float fAvera = 0, fSquSub = 0;
	int j = 0;
	system("cls");
	for (j = 0;j < *i;j++)
	{
		fAvera += (stu + j)->f_totalGrade;
	}
	fAvera /= *i;
	for (j = 0;j < *i;j++)
	{
		fSquSub += ((stu + j)->f_totalGrade - fAvera) * ((stu + j)->f_totalGrade - fAvera);
	}
	fSquSub /= *i;
	printf("平均分为：%.1f\t方差为：%.1f\n", fAvera, fSquSub);
}

//判断正态分布函数
void IfZtfb(struct student* stu, int* i)
{
	float u = 0, fSquSub = 0, sigema = 0, n1 = 0, n2 = 0, n3 = 0;
	int j = 0;
	system("cls");
	for (j = 0;j < *i;j++)
	{
		u += (stu + j)->f_totalGrade;
	}
	u /= *i;
	for (j = 0;j < *i;j++)
	{
		fSquSub += ((stu + j)->f_totalGrade - u) * ((stu + j)->f_totalGrade - u);
	}
	fSquSub /= *i;
	sigema = (float)sqrt((double)fSquSub);
	for (j = 0;j < *i;j++)
	{
		if ((stu + j)->f_totalGrade > (u - sigema) && (stu + j)->f_totalGrade < (u + sigema))
			n1++;
		if ((stu + j)->f_totalGrade > (u - 2 * sigema) && (stu + j)->f_totalGrade < (u + 2 * sigema))
			n2++;
		if ((stu + j)->f_totalGrade > (u - 3 * sigema) && (stu + j)->f_totalGrade < (u + 3 * sigema))
			n3++;
	}
	if (fabs((n1 / *i) - 0.6826) < 0.01)
		if (fabs((n2 / *i) - 0.9544) < 0.01)
			if (fabs((n3 / *i) - 0.9974) < 0.01)
				printf("符合符合正态分布。\n");
			else printf("不符合正态分布。\n");
		else printf("不符合正态分布。\n");
	else printf("不符合正态分布。\n");
}

//显示不同生源地均分排序情况函数
void FromDis(struct student* stu, int* i)
{
	struct from place[34];
	int j = 0, k = 0, m = 0;
	float temp[34] = {0},t = 0;
	//初始化
	for (j = 0;j < 34;j++)
	{
		place[j].e = 0;
		place[j].num = 0;
		place[j].aver = 0;
	}
	//生源地个数统计，将学生按生源地分类
	for (j = 0;j < *i;j++)
	{
		for (k = 0;strcmp((stu + j)->from, place[k].s_Name) && k < 34;) k++;
		if (k >= 34) 
		{
			for (k = 0;place[k].e == 1 && k < 34;) k++;
			strcpy(place[k].s_Name, (stu + j)->from);
			place[k].e = 1;
			place[k].i_num[place[k].num] = j;
			place[k].num++;
		}
		else
		{
			place[k].e = 1;
			place[k].i_num[place[k].num] = j;
			place[k].num++;
		}
	}
	//求均分
	for (j = 0;place[j].e == 1;)
	{
		for (k = 0;k < place[j].num;k++)
		{
			place[j].aver += (stu + place[j].i_num[k])->f_totalGrade;
		}
		place[j].aver /= place[j].num;
		j++;
	}
	//排序
	for (k = 0;k < j;k++)
	{
		temp[k] = place[k].aver;
	}
	for (k = 0;k < j;k++)
	{
		for (m = k + 1;m < j;m++)
		{
			if (temp[k] < temp[m])
			{
				t = temp[k];
				temp[k] = temp[m];
				temp[m] = t;
			}
		}
	}
	//匹配输出
	printf("生源地\t\t平均分\n");
	for (k = 0;k < j;k++)
	{
		for (m = 0;m < j;m++)
		{
			if (place[m].aver == temp[k])
				printf("%s\t\t%.1f\n", place[m].s_Name, place[m].aver);
		}
	}
}